package com.appspile.android.ANMP.applocksexy.application.fragment.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.appspile.android.ANMP.applocksexy.R;
import com.appspile.android.ANMP.applocksexy.application.object.ApplockPackageInfo;
import com.appspile.android.ANMP.applocksexy.commons.LogUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by TRUNG on 2/4/2015.
 */
public class ApplicationManagerAdapter extends BaseAdapter{

    static final String TAG = "Adapter    ";
    private Context mContext;
    private LayoutInflater inflate;
    private List<ApplockPackageInfo> mData ;

    public ApplicationManagerAdapter(Activity context, List<ApplockPackageInfo> data)
    {
        this.mContext = context;
        this.mData = data;
        this.inflate = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public ApplockPackageInfo getItem(int position) {

        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

     public  class ViewHolder {

          ImageView ivIconApp;
          TextView tvAppName;
          TextView tvAppVersion;
     }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View view = convertView;
        ViewHolder holder;

        LogUtils.d(TAG, "set view ============= " +  position);
        if(convertView == null)
        {

            convertView = inflate.inflate(R.layout.row_application_info, null);
            holder = new ViewHolder();
            holder.ivIconApp = (ImageView) convertView.findViewById(R.id.ivIconApp);
            holder.tvAppName = (TextView) convertView.findViewById(R.id.tvAppName);
            holder.tvAppVersion = (TextView) convertView.findViewById(R.id.tvVersionName);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.ivIconApp.setImageDrawable(mData.get(position).getIcon());
        holder.tvAppName.setText(mData.get(position).getAppName());
        holder.tvAppVersion.setText(mData.get(position).getVersionName());

        return convertView;
    }

    public void replaceAllData(List<ApplockPackageInfo> data)
    {

        LogUtils.d(TAG, " ===========  " + data.size());
        if(data != null && 0 < data.size())
        {

            LogUtils.d(TAG, " ===========  " + data.size());
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }
}
